package com.dtu.XMLExtras;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlSerializer;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class WriteXMLPhotoInfo{
	
	HttpURLConnection conn;

	public void createXMLfile(String owner, String tag){
		//create a new file called "new.xml" in the SD card
		File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/photoinfo.xml");
		try{
			newxmlfile.createNewFile();
		}catch(IOException e){
			Log.e("IOException", "exception in createNewFile() method");
		}
		//we have to bind the new file with a FileOutputStream
		FileOutputStream fileos = null;        
		try{
			fileos = new FileOutputStream(newxmlfile);
		}catch(FileNotFoundException e){
			Log.e("FileNotFoundException", "can't create FileOutputStream");
		}
		//we create a XmlSerializer in order to write xml data
		XmlSerializer serializer = Xml.newSerializer();
		try {
			//we set the FileOutputStream as output for the serializer, using UTF-8 encoding
			serializer.setOutput(fileos, "UTF-8");
			//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
			serializer.startDocument(null, Boolean.valueOf(true));
			//set indentation option
			//serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
			//start a tag called "root"

			serializer.startTag(null, "Image");

			serializer.startTag(null, "Owner");
			serializer.text(owner);
			serializer.endTag(null, "Owner");  
			
			serializer.startTag(null, "Tag");
			serializer.text(tag);
			serializer.endTag(null, "Tag");
			
			serializer.endTag(null, "Image");    
			
			serializer.endDocument();
			//write xml data into the FileOutputStream
			serializer.flush();
			//finally we close the file stream
			fileos.close();

		} catch (Exception e) {
			Log.e("Exception","error occurred while creating xml file");
		}
	}
}
